﻿@model IEnumerable<证件信息管理.Models.Licence>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "MyHome", FormMethod.Get))
{
    <span>证件名称:</span>
    @Html.TextBox("name")
    <span>是否需要年审:</span>
    @Html.DropDownList("isReview", ViewBag.isReviewList as SelectList)
    <span>状态:</span>
    @Html.DropDownList("state", ViewBag.stateList as SelectList)
    <input type="submit" value="提交" />
}
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IssueDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.VaildData)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsReview)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReviewDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.State)
        </th>
        <th>
            操作
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IssueDate)
            </td>
            <td>
                @{
                    if (item.VaildData == null)
                    {
                        @:永久
                    }
                    else
                    {
                        var vaildData = item.VaildData;
                        @vaildData
                    }
                }
            </td>
            <td>
                @if (Convert.ToBoolean(item.IsReview))
                {
                    @:需要
                }
                else
                {
                    @:不需要
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ReviewDate)
            </td>
            <td>
                @{
                    var namelist = ViewBag.nameList as Dictionary<int, string>;
                    var name = item.State + namelist.FirstOrDefault(u => u.Key == item.LendUserID).Value;
                    @name
                }
                @if (item.State == "借出" && item.LendUserID == ViewBag.State)
                {
                    <button onclick="ClickUpdate(@item.ID)">点击归还</button>
                }
            </td>
            <td>
                <button onclick="Delete(@item.ID)">删除</button>
                @if (item.LendUserID == null)
                {
                    <button onclick="Update(@item.ID)">借出</button>
                }
            </td>
        </tr>
    }

</table>
@section Scripts {
    <script>
        function Delete(id) {
            $.post("/MyHome/Delete", { id: id }, function (data) {
                switch (data) {
                    case 0:
                        alert("更新失败");
                        break;
                    case 1:
                        location = "/MyHome/Index";
                        break;
                    case 2:
                        console.log("null");
                        break;
                }
            })
        }

        function Update(id) {
            $.post("/MyHome/Update", { id: id }, function (data) {
                switch (data) {
                    case 0:
                        alert("更新失败");
                        break;
                    case 1:
                        location = "/MyHome/Index";
                        break;
                    case 2:
                        console.log("null");
                        break;
                }
            })
        }

        function ClickUpdate(id) {
            $.post("/MyHome/ClickUpdate", { id: id }, function (data) {
                switch (data) {
                    case 0:
                        alert("更新失败");
                        break;
                    case 1:
                        location = "/MyHome/Index";
                        break;
                    case 2:
                        console.log("null");
                        break;
                }
            })
        }
    </script>
}
